var req;
var reqTimeout;
var req_process;
var action;
var waitplease = '<img src="/img/ajax.gif" /> Wait please...';


function refresh() {
  window.location.href=window.location.href;
}

function NewRequest()
{
  req = null;
  if (window.XMLHttpRequest) {

    try
    {
      return new XMLHttpRequest();
    }
    catch (e){}
  }
  else
    if (window.ActiveXObject)
    {
      try
      {
        return new ActiveXObject('Msxml2.XMLHTTP');
      }
      catch (e){}
      
      try
      {
        return new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch (e){}
    }
}

function replace()
{
  switch (action)
  {
    case "login":
    	uloginf();
    	break;
    default:
//        refresh();
  }
}

function processReqChange()
{
  if (req.readyState == 4) 
  {
    clearTimeout(reqTimeout);
    req_process = false;
      // only if "OK"
    if (req.status == 200)
    {
      replace();
    }
    else
    {
      alert("Не удалось получить данные:\n" + req.statusText);
    }
  }  
}

function get(url)
{
  req = NewRequest();
  // если объекст Req не занят
  if (req)
  {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send(null);
    reqTimeout = setTimeout("req.abort();", 5000);
  }
  else
  {
    alert("Браузер не поддерживает передачу данных при помощи JavaScript");
  }
}

function post(url, params)
{
  req = NewRequest();
  // если объекст Req не занят
  if (req)
  {
    req.onreadystatechange = processReqChange;
    req.open("POST", url, true);
    req.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
    req.send(params);
    reqTimeout = setTimeout("req.abort();", 5000);
  }
  else
  {
    alert("Браузер не поддерживает передачу данных при помощи JavaScript");
  }
}

//
// document.cookie="foo=bar; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT";
// setCookie("foo", "bar", "Mon, 01-Jan-2001 00:00:00 GMT", "/");
function setCookie (name, value, expires, path, domain, secure)
{
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name)
{
  var cookie = " " + document.cookie;
  var search = " " + name + "=";
  var setStr = null;
  var offset = 0;
  var end = 0;
  
  if (cookie.length > 0)
  {
    offset = cookie.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = cookie.indexOf(";", offset)
      if (end == -1)
      {
        end = cookie.length;
      }
      setStr = unescape(cookie.substring(offset, end));
    }
  }
  return(setStr);
}


function logout()
{
	setCookie("id", "");
	setCookie("auth", "");
	refresh();
}

function ulogin()
{
	document.getElementById("ajax").innerHTML = waitplease;
	action = "login";
	var login = document.getElementById("login").value; 
	var pwd = document.getElementById("pwd").value;
	var ip = document.getElementById("ip").checked;
	var params = "login=" + encodeURIComponent(login);
	params += "&pwd=" + encodeURIComponent(pwd);
	params += ( ip ? "&ip=1" : "" );
	setTimeout( "post('/login/?ajax=1', '"+params+"');", 2000 );
}
function uloginf()
{
	action = "";
	var res = req.responseText;
	if ( res != "Error" )
	{
		var today = new Date();
		today.setTime( today.getTime() );
		var exp = new Date( today.getTime() + 1000 * 60 * 60 * 24 * 600 );
		exp = exp.toGMTString();
		res = res.split(' ');
		var id = res[0];
		var hash = res[1];
		setCookie('id',    id,    exp, '/');
		setCookie('auth',  hash,  exp, '/');
		refresh();
	}
	else
	{
		document.getElementById("ajax").innerHTML = 'Incorrect login or password.';
	}
}

function indexof(a, item) {
	n = -1;
	for( i=0 ; n==-1 && i<a.length ; i++ ) {
		if ( a[i]==item ) n=i;
	}
	return n;
}




